<script setup lang="ts">
import { ref } from 'vue'
import { useRouter, useRoute } from 'vue-router'

import { mobileRule, passwordRule, verificationCodeRule } from '@/utils/rules'
import { useMobileVerificationCode } from '@/composable'
import { useUserStore } from '@/stores'

import { loginByPassword, loginByMobileVerificationCode } from '@/services/user'
import { showFailToast, showSuccessToast } from 'vant'

const router = useRouter()
const route = useRoute()
const handleClickRight = () => {
  router.push({ path: '/register' })
}

const mobile = ref<string>('13230000083')
const password = ref<string>('abc12345')
const mobileVerificationCode = ref<string>()
const isShowPassword = ref<boolean>(false)
const isPassword = ref<boolean>(true)
const isAgree = ref<boolean>(true)

// todo: 发送验证码
const { form, send, time, isHasSend } = useMobileVerificationCode(mobile, 'login')
const handleSendMobileVerificationCode = async () => {
  await send()
}

const userStore = useUserStore()
const handleSubmit = async () => {
  if (!isAgree.value) {
    return showFailToast('请勾选用户协议')
  }

  isPassword.value
    ? // 密码登录
      userStore.setUser(await (await loginByPassword(mobile.value, password.value)).data)
    : // 验证码登录
      userStore.setUser(
        await (
          await loginByMobileVerificationCode(mobile.value, mobileVerificationCode.value as string)
        ).data
      )

  router.replace({
    // path: (route.query.returnURL as string) || '/user'
    path: route.query.returnURL ? `${route.query.returnURL}` : '/user'
  })

  showSuccessToast('登录成功')
}
</script>

<template>
  <div class="login-page">
    <cp-nav-bar title="登录" right-text="注册" @click-right="handleClickRight"></cp-nav-bar>
    <div class="login-header">
      <template v-if="isPassword">
        <h3>密码登录</h3>
        <a href="javascript:;" @click="isPassword = !isPassword">短信验证码登录<span>></span></a>
      </template>
      <template v-else>
        <h3>短信验证码登录</h3>
        <a href="javascript:;" @click="isPassword = !isPassword">密码登录<span>></span></a>
      </template>
    </div>
    <div class="login-main">
      <van-form @submit="handleSubmit" ref="form">
        <van-field
          type="tel"
          placeholder="请输入手机号码"
          v-model="mobile"
          name="mobile"
          :rules="mobileRule"
        />
        <van-field
          v-if="isPassword"
          :type="isShowPassword ? 'text' : 'password'"
          placeholder="请输入密码"
          v-model="password"
          autocomplete="off"
          name="password"
          :rules="passwordRule"
        >
          <template #button>
            <cp-icon
              @click="isShowPassword = !isShowPassword"
              :name="`login-eye-${isShowPassword ? 'on' : 'off'}`"
            ></cp-icon>
          </template>
        </van-field>
        <van-field
          v-else
          placeholder="请输入验证码"
          v-model="mobileVerificationCode"
          type="text"
          :rules="verificationCodeRule"
        >
          <template #button>
            <span v-if="isHasSend" class="has-sent-btn">{{ time }}秒后发送验证码</span>
            <span v-else class="sent-btn" @click="handleSendMobileVerificationCode"
              >发送验证码</span
            >
          </template>
        </van-field>
        <div class="cp-cell">
          <van-checkbox v-model="isAgree">
            <span>我已同意</span>
            <a href="javascript:;">用户协议</a>
            <span>及</span>
            <a href="javascript:;">隐私条款</a>
          </van-checkbox>
        </div>
        <div class="cp-cell">
          <van-button native-type="submit" block round type="primary">登 录</van-button>
        </div>
        <div class="cp-cell">
          <a href="javascript:;">忘记密码？</a>
        </div>
      </van-form>
    </div>
    <div class="login-footer">
      <van-divider>第三方登录</van-divider>
      <div class="cp-cell">
        <a href="javascript:;">
          <img src="@/assets/qq.svg" />
        </a>
      </div>
    </div>
  </div>
</template>

<style lang="scss" scoped>
.login {
  &-page {
    padding-top: 46px;
  }

  &-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 30px 30px 50px;
    line-height: 1em;

    h3 {
      font-size: 24px;
      line-height: 1em;
      font-weight: 400;
    }

    a {
      font-size: 14px;
      span {
        margin-left: 5px;
      }
    }
  }

  &-main {
    padding: 0 14px;

    .van-checkbox__label a {
      padding: 0 5px;
      color: var(--cp-primary);
    }

    .sent-btn {
      color: var(--cp-primary);
    }

    .has-sent-btn {
      color: rgba(22, 194, 163, 0.5);
    }
  }

  &-footer {
    .cp-cell {
      justify-content: center;

      img {
        display: block;
        box-sizing: border-box;
        width: 44px;
        height: 44px;
        padding: 4px;
      }
    }
  }
}

.cp-cell {
  height: 52px;
  display: flex;
  align-items: center;
  padding: 0 16px;
}
</style>
